package com.woniuxy.handler;

import com.woniuxy.Uitl.OrderUtil;
import com.woniuxy.service.SiteService;
import com.work.framework.pojo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.server.Session;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author Administrator
 * @title: SiteHandler
 * @projectName mashangpai
 * @description: TODO
 * @date 2019/9/1710:23
 */
@RequestMapping("/site")
@RestController
@ResponseBody
@Api(value = "地址类", tags = "订单")
public class SiteHandler {

    @Autowired
    private SiteService siteService;

    private static OrderUtil orderUtil = new OrderUtil();


    @ApiOperation(value = "订单模块", notes = "收货地址查看,session中获得用户ID进行判定")
    @GetMapping("/showSite")
    public ResultDTO showSite(HttpServletRequest request) {
        HttpSession session = request.getSession();
        List<Site> sites = siteService.findSites(session);
        return new ResultDTO(200, "收货地址信息", sites);

    }

    @ApiOperation(value = "订单模块", notes = "收货地址添加,session中获得用户ID进行判定")
    @PostMapping("/addSite")
    public ResultDTO addSite(Site site, HttpServletRequest request) {
        HttpSession session = request.getSession();
        Integer code = siteService.addSite(site,session);
        String massage = orderUtil.state(code);
        return new ResultDTO(code, massage);
    }

    @ApiOperation(value = "订单模块", notes = "收货地址删除,session中获得用户ID进行判定")
    @PostMapping("/deleteSite")
    public ResultDTO deleteSite(Site site, HttpServletRequest request) {
        HttpSession session = request.getSession();
        Integer code = siteService.deleteSite(site,session);
        String massage = orderUtil.state(code);
        return new ResultDTO(code, massage);
    }

    @ApiOperation(value = "订单模块", notes = "设置默认收货地址,session中获得用户ID进行判定 ")
    @PostMapping("/importSite")
    public ResultDTO importSite(Site site, HttpServletRequest request) {
        HttpSession session = request.getSession();
        Integer code = siteService.importSite(site,session);
        String massage = orderUtil.state(code);
        return new ResultDTO(code, massage);
    }

    @ApiOperation(value = "订单模块", notes = "省市查看")
    @GetMapping("/city")
    public ResultDTO city() {
        List<City> cities = siteService.findCities();
        return new ResultDTO(200, "省市信息", cities);
    }

    @ApiOperation(value = "订单模块", notes = "市区联动")
    @GetMapping("/county/{cityid}")
    public ResultDTO county(@PathVariable Integer cityid) {
        List<County> counties = siteService.findCountiesByCityId(cityid);
        return new ResultDTO(200, "市区信息", counties);
    }

}
